﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Water.Web.Controls.Office.Excel.Util
{
    public class Util
    {
        /// <summary>
        /// 得到列用字符表示的名称。如：A,B,AA,AB
        /// </summary>
        /// <param name="col">第几列</param>
        /// <returns>列用字符表示的名称。如：A,B,AA,AB</returns>
        public static string GetColumnCharacterName(int col)
        {
            col -= 1;
            int m = col % 26;
            int n = (int)(col / 26);
            int ch = 'A' + m;
            string _str = ((char)ch).ToString();
            string _character = string.Empty;
            if (n <= 0)
                _character = _str;
            else
                _character = GetColumnCharacterName(n) + _str;
            return _character;
        }
        /// <summary>
        /// 得到单元格用字符表示的名称。如：A1,B1,AA10,AB10
        /// </summary>
        /// <param name="row">第几行</param>
        /// <param name="col">第几列</param>
        /// <returns>单元格用字符表示的名称。如：A1,B1,AA10,AB10</returns>
        public static string GetCellCharacterName(int row, int col)
        {
            return GetColumnCharacterName(col) + row;
        }
        /// <summary>
        /// 得到单元格表示。如：Cells(1,1)
        /// </summary>
        /// <param name="row">第几行</param>
        /// <param name="col">第几列</param>
        /// <returns>单元格表示。如：Cells(1,1)</returns>
        public static string GetCellName(int row, int col)
        {
            return "Cells(" + row + "," + col + ")";
        }
        /// <summary>
        /// 得到区域用字符表示的名称。如：A1:Z10
        /// </summary>
        /// <param name="startRow">开始行</param>
        /// <param name="startCol">开始列</param>
        /// <param name="endRow">结束行</param>
        /// <param name="endCol">结束列</param>
        /// <returns>区域用字符表示的名称。如：A1:Z10</returns>
        public static string GetRangeCharacterName(int startRow, int startCol, int endRow, int endCol)
        {
            return GetCellCharacterName(startRow, startCol) + ":" + GetCellCharacterName(endRow, endCol);
        }

    }
}
